//
//  MoreToolView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/24.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

public enum MoreToolViewType: Int {
    case collection         = 101 //收藏
    case pageSkip           = 102 //跳页
    case inverted           = 103 //倒序查看
    case report             = 104 //举报
    case QQ                 = 105
    case weChat             = 106
    case weChatCircle       = 107
    case Qzone              = 108
    case weibo              = 109
}

protocol MoreToolViewDelegate:class {
    func moreToolView(moreToolView:MoreToolView , didSelectAt button:UIButton)
}

class MoreToolView: UIView {
    fileprivate var backgroundView : UIView!
    fileprivate var contentView : UIView!
    
    fileprivate var shareStrList:[(name:String , imageName:String , tag:MoreToolViewType)] = [(name:"QQ" , imageName:"share_QQ" , tag:MoreToolViewType.QQ) , (name:"QQ空间" , imageName:"share_Qzone" , tag:MoreToolViewType.Qzone) , (name:"微信" , imageName:"share_weChat" , tag:MoreToolViewType.weChat) , (name:"朋友圈" , imageName:"share_weChatCircle" , tag:MoreToolViewType.weChatCircle) ,  (name:"微博" , imageName:"share_weibo" , tag:MoreToolViewType.weibo)]
    
    fileprivate var toolStrList:[(name:String , imageName:String , tag:MoreToolViewType)] = [(name:"收藏" , imageName:"tool_collection" , tag:MoreToolViewType.collection) , (name:"跳楼" , imageName:"tool_pageSkip" , tag:MoreToolViewType.pageSkip) , (name:"倒序查看" , imageName:"tool_inverted" , tag:MoreToolViewType.inverted) , (name:"举报" , imageName:"tool_report" , tag:MoreToolViewType.report)]
    
    fileprivate let shareWidth = AppWidth / 5
    
    weak var delegate:MoreToolViewDelegate!
    
    init(isVideo:Bool , onlyShowShare:Bool = false , onlyShowReport:Bool = false){
        super.init(frame: UIScreen.main.bounds)
        self.initView(isVideo, onlyShowShare , onlyShowReport)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initView(_ isVideo:Bool , _ onlyShowShare:Bool , _ onlyShowReport:Bool){
        
        self.backgroundView = UIView.init(frame: self.frame)
        self.backgroundView.backgroundColor = UIColor.black
        self.addSubview(self.backgroundView)
        
        let cancelRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.handleTap(_:)))
        cancelRecognizer.numberOfTapsRequired = 1
        self.backgroundView.addGestureRecognizer(cancelRecognizer)
        
        self.contentView = UIView()
        self.contentView.alpha = 0
        self.contentView.backgroundColor = UIColor.white
        self.addSubview(self.contentView)
        
        if onlyShowShare{  //仅仅显示分享工具
            self.contentView.frame = CGRect(x: 0, y: AppHeight - 140, width: AppWidth, height: 140)
            
            for i in 0..<self.shareStrList.count{
                let button = UIButton.init(type: .custom)
                button.tag = self.shareStrList[i].tag.rawValue
                button.frame = CGRect.init(x: shareWidth * CGFloat(i), y: 30, width: shareWidth, height: 80)
                button.set(image: UIImage(named: self.shareStrList[i].imageName), title: self.shareStrList[i].name, titlePosition: .bottom, additionalSpacing: 0, state: UIControlState.normal)
                button.setTitleColor(UIColor.m_102Color(), for: .normal)
                button.titleLabel?.font = UIFont.LightDefaultFont(15)
                button.addTarget(self, action: #selector(self.didRowBtnClick(button:)), for: .touchUpInside)
                self.contentView.addSubview(button)
            }
        }else{
            self.contentView.frame = CGRect(x: 0, y: AppHeight - 280, width: AppWidth, height: 280)
            if isVideo{  //视频
                toolStrList = [(name:"收藏" , imageName:"tool_collection" , tag:MoreToolViewType.collection) , (name:"跳楼" , imageName:"tool_pageSkip" , tag:MoreToolViewType.pageSkip) , (name:"举报" , imageName:"tool_report" , tag:MoreToolViewType.report)]
            }
            
            if onlyShowReport{
                toolStrList = [(name:"举报" , imageName:"tool_report" , tag:MoreToolViewType.report)]
            }
            
            for i in 0..<self.toolStrList.count{
                let button = UIButton.init(type: .custom)
                button.tag = self.toolStrList[i].tag.rawValue
                button.frame = CGRect.init(x: shareWidth * CGFloat(i), y: 30, width: shareWidth, height: 80)
                button.set(image: UIImage(named: self.toolStrList[i].imageName), title: self.toolStrList[i].name, titlePosition: .bottom, additionalSpacing: 0, state: UIControlState.normal)
                button.setTitleColor(UIColor.m_102Color(), for: .normal)
                button.titleLabel?.font = UIFont.LightDefaultFont(15)
                button.addTarget(self, action: #selector(self.didRowBtnClick(button:)), for: .touchUpInside)
                self.contentView.addSubview(button)
            }
            
            let lineView = UIView.init(frame: CGRect.init(x: 0, y: 140, width: AppWidth, height: 1))
            lineView.backgroundColor = UIColor.m_lineColor()
            self.contentView.addSubview(lineView)
            
            for i in 0..<self.shareStrList.count{
                let button = UIButton.init(type: .custom)
                button.tag = self.shareStrList[i].tag.rawValue
                button.frame = CGRect.init(x: shareWidth * CGFloat(i), y: 170, width: shareWidth, height: 80)
                button.set(image: UIImage(named: self.shareStrList[i].imageName), title: self.shareStrList[i].name, titlePosition: .bottom, additionalSpacing: 0, state: UIControlState.normal)
                button.setTitleColor(UIColor.m_102Color(), for: .normal)
                button.titleLabel?.font = UIFont.LightDefaultFont(15)
                button.addTarget(self, action: #selector(self.didRowBtnClick(button:)), for: .touchUpInside)
                self.contentView.addSubview(button)
            }
        }
    }
    
    @objc fileprivate func didRowBtnClick(button:UIButton){
        if self.delegate != nil{
            self.delegate!.moreToolView(moreToolView: self, didSelectAt: button)
        }
        self.close()
    }
    
    @objc fileprivate func handleTap(_ gesture:UITapGestureRecognizer) -> Void{
        self.close()
    }
    
    func show(){
        UIApplication.shared.keyWindow?.addSubview(self)
        self.showBackground()
        self.showContentView()
    }
    
    func close() -> Void{
        self.hideContentView()
        self.hideBackground()
    }
    
    fileprivate func showBackground() -> Void{
        self.backgroundView.alpha = 0.0
        UIView.animate(withDuration: 0.5, animations: {
            self.backgroundView.alpha = 0.6
        })
    }
    
    fileprivate func showContentView(){
        UIView.animate(withDuration: 0.5, animations: {
            self.contentView.alpha = 1
        })
    }
    
    fileprivate func hideBackground() -> Void{
        
        UIView.animate(withDuration: 0.5, animations:{
            self.backgroundView.alpha = 0.0
        }, completion: { (animate) in
            self.removeFromSuperview()
        })
    }
    
    fileprivate  func hideContentView() -> Void{
        UIView.animate(withDuration: 0.5, animations: {
            self.contentView.alpha = 0
        })
    }
}
